package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwj.bo.CourseBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.CourseEntity;
import com.zwj.entity.StudentCourseEntity;
import com.zwj.entity.StudentEntity;
import com.zwj.mapper.CourseMapper;
import com.zwj.mapper.StudentCourseMapper;
import com.zwj.mapper.StudentMapper;
import com.zwj.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-30
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseEntity> implements CourseService {
    private final CourseMapper courseMapper;
    private final StudentCourseMapper studentCourseMapper;
    public CourseServiceImpl(CourseMapper courseMapper,StudentCourseMapper studentCourseMapper) {
        this.courseMapper = courseMapper;
        this.studentCourseMapper = studentCourseMapper;
    }
    @Override
    public Result addCourse(CourseEntity courseEntity) {
        //数据校验
        if(courseEntity.getCourseName()==null||courseEntity.getCourseName().equals("")){
            return Result.error().message("课程名不能为空");
        }
        if(courseEntity.getCredit()==null||courseEntity.getCredit()<1){
            return Result.error().message("学分不合法");
        }
        //插入
        courseEntity.setId(null);
        courseEntity.setIsDeleted("0");
        int row = courseMapper.insert(courseEntity);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("添加课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result deleteCourse(CourseBo courseBo) {
        //数据校验
        if(courseBo.getId()==null||courseBo.getId().equals("")){
            return Result.error().message("课程ID不能为空");
        }
        QueryWrapper<StudentCourseEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StudentCourseEntity::getCourseId,courseBo.getId());
        List<StudentCourseEntity> row1 = studentCourseMapper.selectList(wrapper);
        if (row1.size() != 0) {
            return Result.error().message("有学生在使用课程，不能删除");
        }
        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setIsDeleted("1");
        UpdateWrapper<CourseEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseEntity::getId, courseBo.getId()).eq(CourseEntity::getIsDeleted,'0');
        int row = courseMapper.update(courseEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateCourse(CourseEntity courseEntity) {
        //数据校验
        if(courseEntity.getId()==null||courseEntity.getId().equals("")){
            return Result.error().message("课程ID不能为空");
        }
        if(courseEntity.getCourseName()==null||courseEntity.getCourseName().equals("")){
            return Result.error().message("课程名不能为空");
        }
        if(courseEntity.getCredit()==null||courseEntity.getCredit()<1){
            return Result.error().message("学分不合法");
        }
        courseEntity.setIsDeleted(null);
        UpdateWrapper<CourseEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseEntity::getId, courseEntity.getId());
        int row = courseMapper.update(courseEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result seleteCourse(CourseBo courseBo) {
        //数据校验s
        if(!PaginationUtils.isPagingParameters(courseBo.getNumber(), courseBo.getPage())){
            return Result.error().message("分页参数错误!");
        }
        int page = courseBo.getPage();
        courseBo.setPage(PaginationUtils.getPageIndex(courseBo.getPage(), courseBo.getNumber()));
        //查询项目组评分 mybatis
        List<Object> result = courseMapper.seleteCourse(courseBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(courseBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<CourseEntity> courseEntities =  (List<CourseEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,courseEntities ));
    }
}
